package com.mxpio.erp.workshop.controller;

import com.mxpio.erp.common.workshop.entity.MaterialKittingAnalysisHistory;
import com.mxpio.erp.common.workshop.service.MaterialKittingAnalysisHistoryService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "MaterialKittingAnalysisHistoryController", description = "生产订单齐套分析记录接口")
@RestController
@RequestMapping("/erp/workshop/mo/analysis_history/")
public class MaterialKittingAnalysisHistoryController {

    @Autowired
    private MaterialKittingAnalysisHistoryService service;

    @GetMapping("list")
    @Operation(summary = "生产订单齐套分析记录列表", description = "获取生产订单齐套分析记录列表（无分页）", method = "GET")
    public Result<List<MaterialKittingAnalysisHistory>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<MaterialKittingAnalysisHistory> analysisHistory = service.list(MaterialKittingAnalysisHistory.class, criteria);
        return Result.OK(analysisHistory);
    }

    @GetMapping("page")
    @Operation(summary = "生产订单齐套分析记录列表", description = "获取生产订单齐套分析记录列表（分页）", method = "GET")
    public Result<Page<MaterialKittingAnalysisHistory>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<MaterialKittingAnalysisHistory> analysisHistorys = service.listPage(MaterialKittingAnalysisHistory.class, page, criteria);
        return Result.OK(analysisHistorys);
    }

    @GetMapping("last_one")
    @Operation(summary = "生产订单齐套分析最后一次记录", description = "生产订单齐套分析最后一次记录", method = "GET")
    public Result<MaterialKittingAnalysisHistory> list() {
        MaterialKittingAnalysisHistory analysisHistory = service.lastOne();
        if (analysisHistory!=null){
            return Result.OK(analysisHistory);
        }else{
            return Result.error("未查询到分析记录，请先执行齐套分析。");
        }
    }
}
